package com.snow.service.site.SiteServiceImpl;

import com.github.pagehelper.PageHelper;
import com.snow.constant.Types;
import com.snow.dao.AttachDao;
import com.snow.dao.CommentDao;
import com.snow.dao.ContentDao;
import com.snow.dao.MetaDao;
import com.snow.pojo.dto.ArchiveDto;
import com.snow.pojo.dto.MetaDto;
import com.snow.pojo.dto.StatisticsDto;
import com.snow.pojo.dto.cond.CommentCond;
import com.snow.pojo.dto.cond.ContentCond;
import com.snow.pojo.model.CommentDomain;
import com.snow.pojo.model.ContentDomain;
import com.snow.service.site.SiteService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @author: snow
 * @Date: 2020-01-11
 **/
@Service
public class SiteServiceImpl implements SiteService {

    private static Logger logger = Logger.getLogger(SiteServiceImpl.class);
    @Autowired
    private ContentDao contentDao;
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private AttachDao attachDao;
    @Autowired
    private MetaDao metaDao;

    /**
     * 获取评论列表
     * @param limit
     * @return
     */
    @Override
    @Cacheable(value="siteCache",key="'comment_' + #p0")
    public List<CommentDomain> getComments(int limit) {
        logger.debug("Enter get Recently Comments method in siteServiceImpl limit:"+limit);
        if(limit < 0 || limit >10){
            limit = 10;
        }
        PageHelper.startPage(1,limit);
        List<CommentDomain> rs = commentDao.getCommentsByCond(new CommentCond());
        logger.debug("Exit getRecent Comments Method in siteServiceImpl");
        return rs;
    }

    /**
     * 获取最新文章
     * @param limit
     * @return
     */
    @Override
    @Cacheable(value = "siteCache",key="'newArticles_' + #p0")
    public List<ContentDomain> getNewArticles(int limit) {
        logger.debug("Enter get New  Article Methods in siteServiceImpl limit :" + limit );
        if(limit < 0 || limit > 10){
            limit = 10;
        }
        PageHelper.startPage(1,limit);
        List<ContentDomain> rs = contentDao.getArticlesByCond(new ContentCond());
        logger.debug("Exit get New Articles in siteServiceImpl");
        return rs;
    }

    @Override
    @Cacheable(value="siteCache", key="'comment_' + #p0")
    public CommentDomain getCommentById(Integer coid) {
        logger.debug("Enter get comment Method in siteServiceImpl id:" + coid);
        if(coid == null){
            throw new RuntimeException("Argument coid is null ");
        }
        CommentDomain rs = commentDao.getCommentById(coid);
        logger.debug("Exit get Comment by id Method in siteServiceImpl");
        return rs;
    }

    /**
     * 获取后台统计数据
     * @return
     */
    @Override
    @Cacheable(value="siteCache",key="'statistics_'")
    public StatisticsDto getStatics() {
        logger.debug("Enter getStatistics Method in siteServiceImpl");
        Long commentsCount = commentDao.getCommentsCount();
        Long articleCount = contentDao.getArticleCount();
        Long attachCount = attachDao.getAttachCount();
        Long linkCount = metaDao.getMetaCountByType(Types.LINK.getType());


        StatisticsDto statisticsDto = new StatisticsDto();
        statisticsDto.setArticles(articleCount);
        statisticsDto.setComments(commentsCount);
        statisticsDto.setAttaches(attachCount);
        statisticsDto.setLinks(linkCount);
        logger.debug("Exit get Statistic Method in siteServiceImpl");

        return statisticsDto;
    }

    /**
     * 根据条件查询，获取归档列表信息
     * @param contentCond
     * @return
     */

    @Override
    @Cacheable(value="siteCache",key="'simpleArchive_' + #p0")
    public List<ArchiveDto> getSimpleArchive(ContentCond contentCond) {
        logger.debug("Enter getSimpleArchive Method in SiteServiceImpl");
        List<ArchiveDto> archive = contentDao.getArchive(contentCond);
        logger.debug("Exit getSimpleArchive Method in SiteServiceImpl");
        return archive;
    }

    /**
     * 根据查询条件获取归档列表
     * @param contentCond
     * @return
     */
    @Override
    @Cacheable(value="siteCache",key="'archive_'")
    public List<ArchiveDto> getArchive(ContentCond contentCond) {
        logger.debug("Enter getArchive Method in siteServiceImpl ");
        List<ArchiveDto> archive = contentDao.getArchive(contentCond);
        return archive;
    }

    /**
     * 获取分类标签列表
     * @param type
     * @param orderBy
     * @param limit
     * @return
     */
    @Override
    @Cacheable(value="siteCache",key="'metas_'+#p0")
    public List<MetaDto> getMetas(String type, String orderBy, int limit) {
        List<MetaDto> rtList = null;
        logger.debug(String.format("Enter getMetas method in siteServiceImpl with args:type={0},order={1},limit={2}",type,orderBy,limit));
        if(type != null && type.length() > 0){
            if(orderBy != null && orderBy.length() > 0){
                orderBy ="count desc,a.mid desc";
            }
            if(limit < 1 || limit >9999){
                limit = 10;
            }
            HashMap<String, Object> argsMap = new HashMap<>();
            argsMap.put("type", type);
            argsMap.put("order", orderBy);
            argsMap.put("limit", limit);
            rtList = metaDao.selectFromSql(argsMap);
        }

        logger.debug("Exit GetMetas Method in siteService  ");

        return rtList;
    }
}
